<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'get';
if($_RQ['op']=='get'){
	$post=get_postdata();
	if(!$post['url']){
		exi('error');
	}
	if($post['system']){
		$uniacid=1;
	}else{
		$uniacid=$_SESSION['uniacid'];
	}
	$substr4=substr($post['url'],0,4);
	if($substr4=='http'){
		$path='/pages/html/html?url='.$post['url'];
	}else{
		$path=wxapp_links($post['url']);
	}
	if(mb_strlen($path)>128){
		exi('');
	}
	//获取小程序ID
	$id=pdo_getcolumn('core_account_wxapp_info',array('uniacid'=>$uniacid),'default_wxapp');
	if($id){
		$wxapp=pdo_get('core_account_wxapp',array('id'=>$id));
	}else{
		$wxapp=pdo_get('core_account_wxapp',array('uniacid'=>$uniacid));
	}
	if(!$wxapp || !$wxapp['appid'] || !$wxapp['appsecret']){
		exi('error');
	}
	$attachment=pdo_getcolumn('core_account_wxapp_ewm',array('wxapp_id'=>$wxapp['id'],'action'=>$post['url']),'attachment');
	$file = ROOT_AT.$attachment;
	if(!$attachment || !(is_file($file))){
			$data=model('core/wxapp')->appcode($path,$wxapp['appid'],$wxapp['appsecret'],200);
			if(!$data){
				exi('');
			}
			$attachment=model('core/files')->source_save($data,'data','jpg');
			$insert=array(
				'uniacid'=>$uniacid,
				'wxapp_id'=>$wxapp['id'],
				'action'=>$post['url'],
				'attachment'=>$attachment,
			);
			pdo_insert('core_account_wxapp_ewm',$insert);
	}
	$ewm_url=tomedia($attachment);
	exi($ewm_url);
}